//
//  PictureView.m
//  iCarStyle
//
//  Created by liu nian on 14-4-18.
//  Copyright (c) 2014年 liunian. All rights reserved.
//

#import "PictureView.h"
#import "ArticleManager.h"
#import "PictureCell.h"

#define kNumberOfCellPerRow    3
@interface PictureView () <UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;
@end
@implementation PictureView

- (id)initWithFrame:(CGRect)frame Catalog:(CatalogType)type
{
    self = [super initWithFrame:frame];
    if (self)
        {
        // Initialization code
        self.isFirstRequest = YES;
        _catalogType = type;
        self.objectReq = [[ReqObject alloc] initWithCatalogType:type pageNum:1];
        [self.collectionView registerClass:[PictureCell class] forCellWithReuseIdentifier:@"cell"];

        }
    return self;
}

- (void)reloadTable
{
    SEL showNoteSel = @selector(showNoteBtn:);
    [self.collectionView reloadData];

}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.collectionView layoutSubviews];
    [self.collectionView setNeedsLayout];
    [self.collectionView reloadData];
}

#pragma mark -- BaseTaskHallViewDelegate

- (void)loadData{

    [[ArticleManager sharedManager] reloadPicListReqObject:self.objectReq itemsBlock:^(NSArray *items) {
        [self.objectReq setIsLoading:NO];
        [self endRefreshState];
        [self hideLoading];
        self.isFirstRequest = NO;
        if (_footRefreshView.state == RefreshLoading)
            {
            [self endFooterLoadState];
            }
        else
            {
            [self endHeaderRefreshState];
            [self.dataSource removeAllObjects];

            }
        [self showFooter:YES];

        [self.dataSource addObjectsFromArray:items];
        [self requestDataFinished];
        [self reloadTable];

    }];
}
- (void)loadMore
{
    if (self.objectReq.isLoading) {
        return;
    }
    [self.objectReq setIsLoading:YES];
    self.objectReq.pageNum++;
    [self loadData];

}
- (void)reload{
    if (self.objectReq.isLoading) {
        return;
    }
    self.objectReq.pageNum = 1;
    [self.objectReq setIsLoading:YES];
    [self loadData];
}
- (void)showLoading
{
    SEL callBack = @selector(showLoading);
    if(_delegate && [_delegate respondsToSelector:callBack])
        {
        [_delegate performSelector:callBack];
        }
}

- (void)hideLoading
{
    SEL callBack = @selector(hideLoading);
    if(_delegate && [_delegate respondsToSelector:callBack])
        {
        [_delegate performSelector:callBack];
        }
}

//刷新数据,toast 暂无新的任务
- (void)refreshData
{
    if(self.isFirstRequest)
        {
        [self showLoading];
        }
    [self reload];
}

- (void)showNoDataView
{
    SEL callBack = @selector(showNoDataView:);
    if([_delegate respondsToSelector:callBack])
        {
        [_delegate performSelector:callBack withObject:self.collectionView];
        }
}

- (void)hideNoDataView
{
    SEL callBack = @selector(hideNoDataView);
    if([_delegate respondsToSelector:callBack])
        {
        [_delegate performSelector:callBack];
        }
}

- (void)changeNoDataViewState
{
    if(self.isFirstRequest)
        {//如果还没请求则先移除
            [self hideNoDataView];
            return;
        }
    //    if([self.dataSource count] <= 0)
    //    {//没有数据则显示去任务大厅按钮
    //        [self showNoDataView];
    //    }
    //    else
    //    {//有数据则移除nodataView
    //        [self hideNoDataView];
    //    }
}

#pragma mark -- HTTP
- (void)endRefreshState
{
    [self.headRefreshView endRefresh];
}
- (void)requestDataFinished{}
#pragma mark -- getter
- (UICollectionView *)collectionView {
    if (!_collectionView) {

        UICollectionViewFlowLayout *flowRight = [[UICollectionViewFlowLayout alloc] init];
        [flowRight setItemSize:CGSizeMake(floor(self.bounds.size.width/kNumberOfCellPerRow),
                                          floor(self.bounds.size.width/kNumberOfCellPerRow))];
        [flowRight setMinimumInteritemSpacing:1];
        [flowRight setMinimumLineSpacing:1];
        [flowRight setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [flowRight setScrollDirection:UICollectionViewScrollDirectionVertical];

        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowRight];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [self addSubview:_collectionView];
    }
    return _collectionView;
}
- (UIScrollView*)mainList
{
    return self.collectionView;
}
- (void)showMessage:(Article *)article animated:(BOOL)animated {

}

//显示多少行
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//显示多少列
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
//为每个单元设置UI
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];//"bookCell" 对应第三步的cell的identifier
    NSInteger index = (indexPath.section*kNumberOfCellPerRow+indexPath.row);
    if (index < self.dataSource.count) {
        PicModel *picture = [self.dataSource objectAtIndex:index];
        cell.picture = picture;
    }

    return cell;
}

//选择单元格触发事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = (indexPath.section*kNumberOfCellPerRow+indexPath.row);
    if (index < self.dataSource.count) {
        PicModel *picture = [self.dataSource objectAtIndex:index];//2为每行的个数
        [_pDelegate toItemViewControllerWithSource:self.dataSource picture:picture];
    }

}

#pragma mark -- getter
- (void)setCatalogType:(CatalogType)catalogType{
    if (catalogType != _catalogType) {
        _catalogType = catalogType;
    }
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataSource;
}
@end
